package com.chinasofti.Servlet;




//import com.chinasofti.sql.ConnSql;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户界面上传名和密码
        String name = request.getParameter("name");
        String password = request.getParameter("pass");

        //创建cookie
        Cookie cookie=new Cookie("username",name);
        /*cookie.setPath(request.getContextPath()+"/login");*/
        response.addCookie(cookie);

/*
        ConnSql.connect();
        int a=ConnSql.select(name,password);
        System.out.println(a);
*/
        int a =1;
        //返回json数据
        response.setContentType("txt/html;charset=UTF-8");
        JSONObject obj = new JSONObject();
        if (a>0) {
            obj.put("code", 200);
            obj.put("msg", "");
        } else {
            obj.put("code", 500);
            obj.put("msg", "错误");
        }
        response.getWriter().write(obj.toString());
    }
}